package controllers

import (
	"newAi/new_web/models"
	"strings"
)

type SeatController struct {
	BaseController
}

func (self *SeatController) List() {
	self.Data["pageTitle"] = "座席管理"
	self.Data["gid"], _ = self.GetInt64("gid")
	self.display()
}

func (self *SeatController) Extension() {
	self.Data["pageTitle"] = "分机管理"
	self.display()
}

func (self *SeatController) SeatAdd() {
	self.Data["pageTitle"] = "座席添加"
	self.display()
}

func (self *SeatController) SeatGroup() {
	self.Data["pageTitle"] = "座席组"
	self.display()
}

func (self *SeatController) ExtensionAdd() {
	self.Data["pageTitle"] = "分机添加"
	self.display()
}

func (self *SeatController) AjaxSave() {
	extension := new(models.AiExtansion)
	extension.Extension_name = self.GetString("number")
	extension.Extension_number = self.GetString("number")
	extension.Extension_pswd = self.GetString("pswd")
	_, err := models.ExtansionAdd(extension)
	if err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
		return
	}

	self.ajaxMsg("", MSG_OK)
}

//座席添加
func (self *SeatController) SeatAddNo() {
	seat := new(models.AiSeat)
	seat.Agent_name = self.GetString("number")
	seat.Agent_no = self.GetString("number")
	seat.Agent_pass = self.GetString("pswd")
	_, err := models.SeatAdd(seat)
	if err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
		return
	}

	self.ajaxMsg("", MSG_OK)
}

func (self *SeatController) TableList() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	result, count := models.SeatGroupGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["group_name"] = v.Group_name

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

func (self *SeatController) Table() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//查询条件
	filters := make([]interface{}, 0)
	result, count := models.ExtansionGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["extension_name"] = v.Extension_name
		row["reg"],row["seat_state"],row["seat_status"] = judgeState(v.Reg_state, v.Seat_state, v.Seat_status)

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

func judgeState(Reg_state, Seat_state, Seat_status string) (string, string, string) {
	reg := "已注册"
	seat_state := "上线"
	seat_status := "空闲"

	if strings.ToLower(Reg_state) == "unreg" {
		reg = "未注册"
	}
	if strings.ToLower(Seat_state) == "down" {
		seat_state = "下线"
	}
	if strings.ToLower(Seat_status) == "busy" {
		seat_status = "忙"
	}

	return reg, seat_state, seat_status

}

func (self *SeatController) SeatTable() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	gid, _ := self.GetInt64("gid")

	self.pageSize = limit
	//查询条件
	result, count := models.SeatGetList(page, self.pageSize, gid)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["agent_name"] = v.Agent_name
		row["extension"] = v.Agent_externsion

		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}

//删除
func (self *SeatController) AjaxDel() {
	id, _ := self.GetInt("id")
	seat, _ := models.ExtansionGetById(id)

	if err := seat.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}

	self.ajaxMsg("", MSG_OK)
}
